import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


/**
 * @author Darren
 * @date 2022-08-10 14:03
 * Description：
 */
public class MallGenerateApplication {
    public static void main(String[] args) {
        // 需要生成的表
//        List<String> generateTables = Arrays.asList("");

        // 数据库配置和代码生成器
        String url = "jdbc:mysql://192.168.100.130:3306/shop_permission?useUnicode=true&useSSL=false&characterEncoding=utf8";
        String username = "root";
        String password = "123456";
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(url, username, password).build();
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig);

        // 全局配置
        String modelPath = "/mall-service/mall-permission-service";
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .outputDir(System.getProperty("user.dir") + modelPath + "/src/main/java")
                .author("Darren")
                .disableOpenDir()
                .enableSwagger()
                .dateType(DateType.ONLY_DATE)
                .build();
        autoGenerator.global(globalConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.mall.permission")
                .entity("model")
                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + modelPath + "/src/main/resources/mapper"))
                .build();
        autoGenerator.packageInfo(packageConfig);

        // 模板配置
        TemplateConfig templateConfig = new TemplateConfig.Builder()
                .entity("/templates/entity.java")
                .mapper("/templates/mapper.java")
                .mapperXml("/templates/mapper.xml")
                .service("/templates/service.java")
                .serviceImpl("/templates/serviceImpl.java")
                .controller("/templates/controller.java")
                .build();
        autoGenerator.template(templateConfig);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .addTablePrefix(packageConfig.getModuleName() + "_")
//                .addInclude(generateTables)
                .entityBuilder()
                    .enableChainModel()
                    .enableLombok()
                    .enableTableFieldAnnotation()
                    .naming(NamingStrategy.underline_to_camel)
                    .columnNaming(NamingStrategy.underline_to_camel)
                    .addTableFills(new Column("create_time", FieldFill.INSERT))
                    .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                    .idType(IdType.AUTO)
                .mapperBuilder()
                    .enableMapperAnnotation()
                    .enableBaseResultMap()
                    .enableBaseColumnList()
                .controllerBuilder()
                    .enableHyphenStyle()
                    .enableRestStyle()
                .build();
        autoGenerator.strategy(strategyConfig);

        // 生成
        autoGenerator.execute();
        System.out.println("=====生成成功!=====");
    }
}
